package cn.agriculture.web.controller;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpSession;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mobile.device.Device;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.agriculture.web.form.CartForm;
import cn.agriculture.web.form.GoodsForm;
import cn.agriculture.web.form.UVO;
import cn.agriculture.web.service.CartService;
import cn.agriculture.web.service.GoodsService;

@Slf4j
@Controller("GoodsController")
@RequestMapping("/")
public class GoodsController {

	@Autowired
	GoodsService goodsService;
	
	@Autowired
	CartService cartService;
	
    @RequestMapping(value = "initGoods", method = RequestMethod.GET)
    public String initGoods(Model model, HttpSession session, GoodsForm goodsForm, Device device) throws UnsupportedEncodingException {
    	log.info("商品列表初始化");
    	String type = new String(goodsForm.getType().getBytes("iso8859-1"),"utf-8");
    	switch(type) {
    	case "liangshi":
    		goodsForm.setType("粮食");
    		break;
    	case "tiaoliao":
    		goodsForm.setType("调料");
    		break;
    	case "huazhuangpin":
    		goodsForm.setType("化妆品");
    		break;
    	case "chongyin":
    		goodsForm.setType("冲饮");
    		break;
    	case "lingshi":
    		goodsForm.setType("零食");
    		break;
    	case "mingcha":
    		goodsForm.setType("茗茶");
    		break;
    	default:
    		goodsForm.setType("粮食");
    		break;
    	}
    	model.addAttribute("goodsForm", goodsForm);
    	model.addAttribute("list", goodsService.searchGoodsList(goodsForm));
    	UVO uvo = (UVO)session.getAttribute("UVO");
    	if (uvo == null) {
    		uvo = new UVO();
    		session.setAttribute("UVO", uvo);
    	}
    	CartForm cartForm = new CartForm();
    	cartForm.setGuestId(uvo.getGuestId());
    	model.addAttribute("cartList", cartService.searchCartList(cartForm));
    	if(device.isNormal()) {
    		return "shop/index";
    	} else {
    		return "mobile/index";
    	}
    }
    
    @RequestMapping(value = "initGoodsDetail", method = RequestMethod.GET)
    public String initGoodsDetail(Model model, HttpSession session, GoodsForm goodsForm, Device device) {
    	log.info("商品明细初始化");
    	UVO uvo = (UVO)session.getAttribute("UVO");
    	if (uvo == null) {
    		uvo = new UVO();
    		session.setAttribute("UVO", uvo);
    	}
    	CartForm cartForm = new CartForm();
    	cartForm.setGuestId(uvo.getGuestId());
    	model.addAttribute("cartList", cartService.searchCartList(cartForm));
    	model.addAttribute("goodsForm", goodsService.searchGoods(goodsForm));
    	if(device.isNormal()) {
    		return "shop/goods/goodsDetail";
    	} else {
    		return "mobile/goods/goodsDetail";
    	}
    }
}
